package com.aaa.hospital.controller;

import com.aaa.hospital.pojo.domain.Jianyi;
import com.aaa.hospital.pojo.query.JianyiQuery;
import com.aaa.hospital.result.AjaxResult;
import com.aaa.hospital.service.PatientService;
import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * Author:张海金
 * Description:T0D0
 * DateTime:2022/2/23 14:36
 **/
@Controller
@RequestMapping("export")
public class PatientExportController {

    @Autowired
    private PatientService patientService;

    @RequestMapping("download")
    public void downloadStudentExcel(JianyiQuery   patient, HttpServletResponse response) throws IOException {

        // 1 获取数据
        AjaxResult result = patientService.queryAll(patient);

        Map map = (Map) result.getData();

        List<Jianyi> pageData = (List<Jianyi>) map.get("pageData");

        // 2 下载excel

        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("学生信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        EasyExcel.write(   response.getOutputStream()  , Jianyi.class    )     .sheet("学生信息").doWrite( pageData      );
    }


}